package com.masterclient.app.businessmodule.homemodule.view.adapter.feature;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.commonbean.banner.BannerNewItemBean;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.banner.BannerNewBean;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselibrary.utils.UtilCollection;


public class HomePageXFeatureAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private BannerNewBean featureBean;
    private EventAdDataCallback eventAdDataCallback;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public HomePageXFeatureAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null);
    }

    public HomePageXFeatureAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, BannerNewBean featureBean) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.featureBean = featureBean;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        ImageView ivFeature = holder.itemView.findViewById(R.id.iv_feature);
        ivFeature.getLayoutParams().height = ScreenUtils.getScreenWidth()*8/75;


        if (featureBean.getList() != null && featureBean.getList().size() > 0) {
            ivFeature.setVisibility(View.VISIBLE);
            BannerNewItemBean shFeaturesBean = featureBean.getList().get(0);
            if (UtilCollection.isColor(featureBean.getBg_color())) {
                ivFeature.setBackgroundColor(Color.parseColor(featureBean.getBg_color()));
            } else {
                ivFeature.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }
            //TODO:粉丝福利、新人红包。。。是一张图片
            ViewTransformUtil.glideImageView(mContext, shFeaturesBean.getImage(), ivFeature,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
//            GlideUtil.displayImageFitCenter(mContext, shFeaturesBean.getImage(), ivFeature, R.mipmap.defaults_1, ScreenUtils.getScreenWidth(),ivFeature.getLayoutParams().height);
            //App.picasso.load(shFeaturesBean.getSh_image()).resize(ScreenUtils.getScreenWidth(), ScreenUtils.dp2px(40)).into(ivFeature);
            ivFeature.setOnClickListener(v -> {
                eventAdDataCallback.adData(shFeaturesBean.getS_ad_name(),shFeaturesBean.getS_ad_id(),shFeaturesBean.getS_group_name(),shFeaturesBean.getS_group_id(),
                        shFeaturesBean.getTarget(), shFeaturesBean.getTarget_id(),"0");
                CommonUtil.goNewAnyWhere(mContext,shFeaturesBean.getTarget_platform(),shFeaturesBean.getTarget_message(), shFeaturesBean.getTarget(), shFeaturesBean.getTarget_id(),
                        shFeaturesBean.getImage(),shFeaturesBean.getS_ad_name(), shFeaturesBean.getS_ad_id());
            });
        } else {
            ivFeature.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 5;
    }
}
